//
//  YeeRefreshGifHeader.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/2/27.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeRefreshGifHeader.h"
#import "NSBundle+MJRefresh.h"
@interface YeeRefreshGifHeader ()

@property (weak, nonatomic) UIImageView *loading;
@end
@implementation YeeRefreshGifHeader

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
#pragma mark - 懒加载子控件
- (UIImageView *)loading
{
    if (_loading==nil)
    {
        UIImageView *arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_imgBlue_78x78"]];
        
        [self addSubview:_loading = arrowView];
    }
    return _loading;
}
- (void)prepare
{
    [super prepare];
    self.labelLeftInset=20;
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    // 箭头的中心点
    CGFloat arrowCenterX = self.mj_w * 0.5;
    if (!self.stateLabel.hidden) {
        CGFloat stateWidth = self.stateLabel.mj_textWith;
        CGFloat timeWidth = 0.0;
        if (!self.lastUpdatedTimeLabel.hidden) {
            timeWidth = self.lastUpdatedTimeLabel.mj_textWith;
        }
        CGFloat textWidth = MAX(stateWidth, timeWidth);
        arrowCenterX -= textWidth / 2 + self.labelLeftInset;
    }
    CGFloat arrowCenterY = self.mj_h * 0.5;
    CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
    
    // 箭头
    if (self.loading.constraints.count == 0) {
        self.loading.mj_size = CGSizeMake(50, 50);
        self.loading.center = arrowCenter;
    }

   
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    // 根据状态做事情
    if (state == MJRefreshStateIdle) {
        if (oldState == MJRefreshStateRefreshing)
        {
            [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                self.loading.alpha = 0.0;
                
            } completion:^(BOOL finished) {
                // 如果执行完动画发现不是idle状态，就直接返回，进入其他状态
                if (self.state != MJRefreshStateIdle) return;
                self.loading.alpha = 1.0;
                [self.loading .layer removeAllAnimations];
            }];
        } else
        {
            [self.loading.layer removeAllAnimations];
            self.loading.alpha=0.0;
           // self.loading .hidden=YES;
        }
    } else if (state == MJRefreshStatePulling)
    {
        [self.loading.layer removeAllAnimations];
        self.loading.alpha=0.0;
        //self.loading .hidden=YES;
    } else if (state == MJRefreshStateRefreshing) {
     self.loading.alpha = 1.0; // 防止refreshing -> idle的动画完毕动作没有被执行
     [self.loading.layer addAnimation:[self animation] forKey:@"animation"];
    }
}
-(CABasicAnimation*)animation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    return animation;
}
@end
